//
//  WYCDetailCardController.m
//  信用卡管理
//
//  Created by ZX on 2017/11/22.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCHandBillController.h"
#import "CustomPickerView.h"
#import "WYCDayPickerView.h"
#import "PickerDateViewController.h"
#import "MyTextField.h"
#import "RSAUtil.h"
#import "WYCBillController.h"
#import "WYCRepaymentController.h"
#import "WYCBillModel.h"
#import "WYCCardlistPickerView.h"


@interface WYCHandBillController ()
{
    UIButton * cardbutton;
    
    NSString * str;
    UIButton * daybutton;
    
    UILabel * backLabel;
    
    
    
}
/** 数组*/
@property(nonatomic,strong)NSMutableArray * billarray;


@property (nonatomic,weak) WYCCardlistPickerView *PickerView;

@property(nonatomic,strong)CALayer *bottomLineLayer;

@property (nonatomic,weak) UILabel *name;


@property (nonatomic,weak) UILabel *bank;

/** 银行*/
@property(nonatomic,strong)UILabel * money;

/** 姓名*/
@property(nonatomic,strong)UILabel * bankname;

/** 账单日*/
@property(nonatomic,strong)UILabel * bill;


/** 信用额度*/
@property(nonatomic,strong)UITextField * Creditline;


/** 账单金额*/
@property(nonatomic,strong)UITextField * Billamount;


/** 金额*/
@property(nonatomic,strong)MyTextField * moneyField;

/** 账单日*/
@property(nonatomic,strong)UIButton * check;

/** 按钮选择*/
@property(nonatomic,strong)WYCDayPickerView *DayPickerView;

@end

@implementation WYCHandBillController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WYCGlobalBg;
    
    [self setupCreat];
    
    self.billarray = [[NSMutableArray alloc] init];
    
    self.navigationItem.title = @"手输账单";
    
    //获取信用卡列表
    [self setupCreditcardlist];
    
    
}
- (void)setupCreditcardlist{
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"-------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                WYCLog(@"========%@",model.billDate);
                model.bank = [NSString stringWithFormat:@"%@(%@)",model.bankName,model.lastNumber];
                [self.billarray addObject:model];
            }
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
- (void)setupCreat {
    
    
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 1080 * px);
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    //姓名
    UILabel * cardLabel = [[UILabel alloc] init];
    cardLabel.frame = CGRectMake(40 * px, WYCClearW, 500 * px, WYCClearH);
    cardLabel.text = @"信用卡号后四位:";
    cardLabel.font = [UIFont systemFontOfSize:WyzFont];
    [backView addSubview:cardLabel];
    
    //赋值控件
    UILabel * card = [[UILabel alloc] init];
    card.frame = CGRectMake(CGRectGetMaxX(cardLabel.frame) + WYCClearW, cardLabel.y, ScreenWidth / 2, cardLabel.height);
    card.font = forLight;
    [backView addSubview:card];
    
    
    cardbutton = [[UIButton alloc] init];
    cardbutton.frame = CGRectMake(backView.width / 2, cardLabel.y, backView.width / 2 - 80 * px, cardLabel.height);
    [cardbutton setTitle:@"请选择信用卡 >" forState:UIControlStateNormal];
    cardbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [cardbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    cardbutton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [cardbutton addTarget:self action:@selector(cardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cardbutton];
    
    
    UIView * cardview = [[UIView alloc] init];
    cardview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    cardview.backgroundColor = WYCgrayColor;
    [backView addSubview:cardview];
    
    //姓名
    UILabel * moneyLabel = [[UILabel alloc] init];
    moneyLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    moneyLabel.text = @"银行:";
    moneyLabel.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:moneyLabel];
    
    
    //姓名
    self.money = [[UILabel alloc] init];
    self.money.frame = CGRectMake(CGRectGetMaxX(cardLabel.frame) , moneyLabel.y, backView.width - moneyLabel.width - 160 * px, WYCClearH);
    self.money.textAlignment = NSTextAlignmentRight;
    self.money.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:self.money];
    
    
    UIView * moneyview = [[UIView alloc] init];
    moneyview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(moneyLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    moneyview.backgroundColor = WYCgrayColor;
    [backView addSubview:moneyview];
    
    //姓名
    UILabel * nameLabel = [[UILabel alloc] init];
    nameLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(moneyview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    nameLabel.text = @"姓名:";
    nameLabel.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:nameLabel];
    
    
    //姓名
    self.bankname = [[UILabel alloc] init];
    self.bankname.frame = CGRectMake(CGRectGetMaxX(nameLabel.frame) , nameLabel.y, self.money.width, WYCClearH);
    //    self.bankname.text = @"1111";
    self.bankname.textAlignment = NSTextAlignmentRight;
    self.bankname.font = [UIFont systemFontOfSize:WyzFont];
    [backView addSubview:self.bankname];
    
    
    UIView * billview = [[UIView alloc] init];
    billview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(nameLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    billview.backgroundColor = WYCgrayColor;
    [backView addSubview:billview];
    
    //姓名
    UILabel * billLabel = [[UILabel alloc] init];
    billLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(billview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    billLabel.text = @"账单日:";
    billLabel.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:billLabel];
    
    
    
    //赋值控件
    UIButton * check = [[UIButton alloc] init];
    check.frame = CGRectMake(CGRectGetMaxX(billLabel.frame) , billLabel.y, self.money.width, WYCClearH);
    [check addTarget:self action:@selector(chekClick) forControlEvents:UIControlEventTouchUpInside];
    [check setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [check setTitle:@"选择账单日 >" forState:UIControlStateNormal];
    check.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    check.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [backView addSubview:check];
    self.check = check;
    
    //姓名
    self.bill = [[UILabel alloc] init];
    self.bill.frame = CGRectMake(CGRectGetMaxX(billLabel.frame) , billLabel.y, self.money.width, WYCClearH);
    //    self.bill.text = @"1111";
    self.bill.textAlignment = NSTextAlignmentRight;
    self.bill.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:self.bill];
    
    
    UIView * Creditlineview = [[UIView alloc] init];
    Creditlineview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(billLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    Creditlineview.backgroundColor = WYCgrayColor;
    [backView addSubview:Creditlineview];
    
    //姓名
    UILabel * CreditlineLabel = [[UILabel alloc] init];
    CreditlineLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(Creditlineview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    CreditlineLabel.text = @"信用额度:";
    CreditlineLabel.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:CreditlineLabel];
    
    
    //姓名
    self.Creditline = [[UITextField alloc] init];
    self.Creditline.frame = CGRectMake(CGRectGetMaxX(CreditlineLabel.frame) , CreditlineLabel.y, self.money.width, WYCClearH);
    //    Creditline.text = @"1111";
    self.Creditline.textAlignment = NSTextAlignmentRight;
    self.Creditline.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:self.Creditline];
    
    
    UIView * Billamountview = [[UIView alloc] init];
    Billamountview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(CreditlineLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    Billamountview.backgroundColor = WYCgrayColor;
    [backView addSubview:Billamountview];
    
    //姓名
    UILabel * BillamountLabel = [[UILabel alloc] init];
    BillamountLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(Billamountview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    BillamountLabel.text = @"账单金额:";
    BillamountLabel.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:BillamountLabel];
    
    
    //姓名
    self.Billamount = [[MyTextField alloc] init];
    self.Billamount.frame = CGRectMake(CGRectGetMaxX(BillamountLabel.frame) , BillamountLabel.y, self.money.width, WYCClearH);
    //    self.Billamount.text = @"1111";
    self.Billamount.textAlignment = NSTextAlignmentRight;
    self.Billamount.font = [UIFont systemFontOfSize:WyzFont];;
    [backView addSubview:self.Billamount];
    
    
    UIButton * implementButton = [[UIButton alloc] init];
    implementButton.frame = CGRectMake(40 * px, CGRectGetMaxY(backView.frame) + 80 * px, ScreenWidth - 80 * px, 150 * px);
    implementButton.layer.cornerRadius = 10 * px;
    implementButton.layer.masksToBounds = YES;
    implementButton.backgroundColor = WYCMain;
    [implementButton setTitle:@"保存" forState:UIControlStateNormal];
    [implementButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [implementButton addTarget:self action:@selector(impleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:implementButton];
    
}
- (void)chekClick{
    
    WYCLogFunc;
    NSMutableArray *checkArray = [NSMutableArray new];
    for (int i = 0; i < 28; i ++) {
        [checkArray addObject:[NSString stringWithFormat:@"%d日",i+1]];
    }
    
    self.DayPickerView = [[WYCDayPickerView alloc]initWithSourceData:checkArray confirmBlock:^(NSString *dayStr) {
        [self.check setTitle:dayStr forState:UIControlStateNormal];
        [self.view endEditing:YES];
        
        [self hideView];
        
    } cancelBlock:^(NSString *dayStr) {
        [self.view endEditing:YES];
        [self hideView];
    }];
    
    
    self.DayPickerView.frame  = CGRectMake(0, ScreenHeight - 300, ScreenWidth, 300);
    [self.view addSubview:self.DayPickerView];
}
- (void)hideView{
    
    [self.DayPickerView setHidden:YES];
    
    
}
- (void)cardButtonClick
{
    
    WYCLogFunc;
    if (!self.PickerView) {
        
        NSMutableArray *arr = self.billarray;
        WYCCardlistPickerView *PickerView = [[WYCCardlistPickerView alloc]initWithSourceData:arr confirmBlock:^(WYCBillModel * BillModel) {
            
            NSString * Str = [NSString stringWithFormat:@"%@(%@)>",BillModel.bankName,BillModel.lastNumber];
            [cardbutton setTitle:Str forState:UIControlStateNormal];
            //            self.name.text = BillModel.name;
            self.bank.text = BillModel.bankId;
            self.bankname.text = BillModel.name;
            self.money.text = BillModel.bankName;
            WYCLog(@"1111111111%@",BillModel.billDate);
            NSArray * arr1 = [BillModel.billDate componentsSeparatedByString:@"月"];
            WYCLog(@"---------%@",arr1[1]);
            //            self.bill.text = [NSString stringWithFormat:@"%@日",BillModel.outbBllDay];
            [self.check setTitle:[NSString stringWithFormat:@"%@ >",arr1[1]] forState:UIControlStateNormal];
            self.Creditline.text = BillModel.creditLine;
            str = BillModel.cardId;
            [self pickerViewhide:YES];
            
            
        } cancelBlock:^(WYCBillModel * BillModel) {
            
            [self pickerViewhide:YES];
            
        }];
        PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 220);
        [self.view addSubview:PickerView];
        self.PickerView = PickerView;
        [self pickerViewhide:NO];
    }else{
        
        [self pickerViewhide:NO];
    }
}

- (void)pickerViewhide:(BOOL)hide {
    
    if (hide) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 280);
        }];
    }else {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight - 280, ScreenWidth, 280);
        }];
    }
    
}

- (void)impleButtonClick{
    
    NSString *cccc = [self.check.titleLabel.text substringToIndex:[self.check.titleLabel.text length] - 1];
    
    NSString *strUrl = [cccc stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = str;
    params[@"billDate"] = strUrl;
    params[@"creditLine"] = self.Creditline.text;
    params[@"billAmount"] = self.Billamount.text;
    WYCLog(@"---------%@",params);
    [HttpTool POST:ImportBillManual params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"成功"];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCMain;
    [self.view addSubview:NavView];
    
    
    
    self.bottomLineLayer = [[CALayer alloc] init];
    self.bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    self.bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:self.bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"手输账单";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end

